/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import exatrez.dao.PenggunaDAO;
import exatrez.dao.UserGroupDAO;
import exatrez.model.User;
import exatrez.model.UserGroup;
import exatrez.stripes.util.EncyptUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author fikri
 */
public class UserManager {

    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    @Inject
    private PenggunaDAO penggunaDAO;
    @Inject
    private EncyptUtil encyptUtil;
    @Inject
    private UserGroupDAO userGroupDAO;

    public boolean authenticate(String userId, String password) {

        String encrypted = encyptUtil.encrypt(password);
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select p from User p "
                + "where userId = :userId and password = :password");
        query.setString("userId", userId);
        query.setString("password", encrypted);
        User user = (User) query.uniqueResult();
        return user != null;
    }

    public List<User> findAll() {
        String query = "SELECT a FROM User a WHERE a.userId is not null";
        Query q = sessionProvider.get().createQuery(query);
        return q.list();
    }
    
    public List<User> findP() {
        String query = "SELECT a FROM User a WHERE a.userId is not null and activate = 'Y' ";
        Query q = sessionProvider.get().createQuery(query);
        return q.list();
    }

    public UserGroup findUserIdGroup(String userId , String roles) {
        String query = "SELECT a FROM UserGroup a WHERE a.user.userId = :userId and a.group.groupId = :roles";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("userId", userId);
        q.setString("roles", roles);
        return (UserGroup) q.uniqueResult();
    }
    
    public UserGroup findUserId(String userId) {
        String query = "SELECT a FROM UserGroup a WHERE a.user.userId = :userId";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("userId", userId);
        return (UserGroup) q.uniqueResult();
    }

    @Transactional
    public void saveOrUpdateUser(User user) {
        penggunaDAO.saveOrUpdate(user);
    }
    
    @Transactional
    public void deleteUser(User user) {
        penggunaDAO.delete(user);
    }

    @Transactional
    public void saveOrUpdateGroup(UserGroup userGroup) {
        userGroupDAO.saveOrUpdate(userGroup);
    }
    
    @Transactional
    public void deleteUserGroup(UserGroup userGroup) {
        userGroupDAO.delete(userGroup);
    }

    public PenggunaDAO getPenggunaDAO() {
        return penggunaDAO;
    }

    public void setPenggunaDAO(PenggunaDAO penggunaDAO) {
        this.penggunaDAO = penggunaDAO;
    }

    public EncyptUtil getEncyptUtil() {
        return encyptUtil;
    }

    public void setEncyptUtil(EncyptUtil encyptUtil) {
        this.encyptUtil = encyptUtil;
    }

    public UserGroupDAO getUserGroupDAO() {
        return userGroupDAO;
    }

    public void setUserGroupDAO(UserGroupDAO userGroupDAO) {
        this.userGroupDAO = userGroupDAO;
    }
}
